﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;

namespace Expressionista.UserControls
{
	/// <summary>
	/// Interaction logic for ReplacerView.xaml
	/// </summary>
	public partial class ReplacerView : UserControl
	{
		public static readonly RoutedEvent ReplacerButtonClickEvent = EventManager.RegisterRoutedEvent("ReplacerButtonClick", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ReplacerView));

		public event RoutedEventHandler ReplacerButtonClick
		{
			add
			{
				AddHandler(ReplacerButtonClickEvent, value);
			}
			remove
			{
				RemoveHandler(ReplacerButtonClickEvent, value);
			}
		}

		public ReplacerView()
		{
			InitializeComponent();
		}

		private void ReplaceBTN_Click(object sender, RoutedEventArgs e)
		{
			RaiseReplacerButtonClick();
		}

		private void RaiseReplacerButtonClick()
		{
			RoutedEventArgs args = new RoutedEventArgs();
			args.RoutedEvent = ReplacerView.ReplacerButtonClickEvent;
			args.Source = this;
			RaiseEvent(args);
		}

		internal void ProcessReplace(string regexStr, RegexOptions regexOptions, string sourceStr)
		{
			try
			{
				Regex re = new Regex(regexStr, regexOptions);
				string replaced = re.Replace(sourceStr, ReplaceWithTB.Text);
				ReplacerTB.Text = replaced;
			}
			catch (Exception e)
			{
				ReplacerTB.Text = string.Format("Error processing Regex.Replace: {0}", e);
			}
		}
	}
}
